/**
 * 
 */
package info.niwota.commons.web;

import info.niwota.commons.Debug;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.util.Log;

/**
 * Added convenience get methods with additional default parameter and without
 * throwing exceptions
 * 
 * @author qiangli
 * 
 */
public class JsonObject extends JSONObject {
	public static boolean debug = Debug.DEBUG;

	private static final String TAG = "JsonObject";

	/**
	 * 
	 */
	public JsonObject() {
	}

	/**
	 * @param readFrom
	 * @throws JSONException
	 */
	public JsonObject(JSONTokener readFrom) throws JSONException {
		super(readFrom);
	}

	/**
	 * @param copyFrom
	 */
	public JsonObject(Map<?, ?> copyFrom) {
		super(copyFrom);
	}

	/**
	 * @param json
	 * @throws JSONException
	 */
	public JsonObject(String json) throws JSONException {
		super(json);
	}

	/**
	 * @param copyFrom
	 * @param names
	 * @throws JSONException
	 */
	public JsonObject(JSONObject copyFrom, String[] names) throws JSONException {
		super(copyFrom, names);
	}

	
	@Override
	public Object get(String name) {
		try {
			return super.get(name);
		} catch (JSONException e) {
			return null;
		}
	}

	@Override
	public boolean getBoolean(String name) {
		try {
			return super.getBoolean(name);
		} catch (JSONException e) {
			return false;
		}
	}

	@Override
	public double getDouble(String name) {
		try {
			return super.getDouble(name);
		} catch (JSONException e) {
			return 0;
		}
	}

	@Override
	public int getInt(String name) {
		try {
			return super.getInt(name);
		} catch (JSONException e) {
			return 0;
		}
	}

	@Override
	public JSONArray getJSONArray(String name) {
		try {
			return super.getJSONArray(name);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public JSONObject getJSONObject(String name) {
		try {
			return super.getJSONObject(name);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public long getLong(String name) {
		
		try {
			return super.getLong(name);
		} catch (JSONException e) {
			return 0;
		}
	}

	@Override
	public String getString(String name) {

		try {
			return super.getString(name);
		} catch (JSONException e) {
			return null;
		}
	}

	public String getString(String key, String def) {
		try {
			return super.getString(key);
		} catch (JSONException e) {
			if (debug) {
				Log.e(TAG, e.getMessage());
			}
		}
		return def;
	}

	public double getDouble(String key, double def) {
		try {
			return super.getDouble(key);
		} catch (JSONException e) {
			if (debug) {
				Log.e(TAG, e.getMessage());
			}
		}
		return def;
	}

	public int getInt(String key, int def) {
		try {
			return super.getInt(key);
		} catch (JSONException e) {
			if (debug) {
				Log.e(TAG, e.getMessage());
			}
		}
		return def;
	}

	public long getLong(String key, long def) {
		try {
			return super.getLong(key);
		} catch (Exception e) {
			if (debug) {
				Log.e(TAG, e.getMessage());
			}
		}
		return def;
	}

	public boolean getBoolean(String key, boolean def) {
		try {
			return super.getBoolean(key);
		} catch (JSONException e) {
			if (debug) {
				Log.e(TAG, e.getMessage());
			}
		}
		return def;
	}
}
